package org.example.task.service.impl;

import lombok.RequiredArgsConstructor;
import org.example.task.converter.RoleConverterMapper;
import org.example.task.model.dto.role.AssignRole;
import org.example.task.model.dto.role.CreateRole;
import org.example.task.model.po.Role;
import org.example.task.mapper.RoleMapper;
import org.example.task.model.po.User;
import org.example.task.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.task.service.IUserService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-26
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    private final RoleConverterMapper roleConverterMapper;
    private final IUserService userService;

    @Override
    public boolean addRole(CreateRole createRole) {
        Role role = roleConverterMapper.createRoleToRole(createRole);
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        return save(role);
    }

    @Override
    public boolean assignRole(AssignRole assignRole) {
        return userService
                .lambdaUpdate()
                .eq(User::getUserId, assignRole.getUserId())
                .update();
    }
}
